import { clearCookie, getCookie, setCookie } from 'qx-util';
import { isUniMob } from '../uni-util/uni-util';

/**
 * cookie 工具类
 *
 * 适配uniapp环境
 * @export
 * @class CookieUtil
 */
export class CookieUtil {
  /**
   * 清除cookie
   *
   * @static
   * @param {string} cookieName
   * @param {boolean} [isDomain]
   * @memberof CookieUtil
   */
  static clearCookie(cookieName: string, isDomain?: boolean): void {
    if (isUniMob()) {
      uni.removeStorageSync(cookieName);
    } else {
      clearCookie(cookieName, isDomain);
    }
  }

  /**
   * 获取cookie
   *
   * @static
   * @param {string} name
   * @return {*}  {(string | null)}
   * @memberof CookieUtil
   */
  static getCookie(name: string): string | null {
    if (isUniMob()) {
      return uni.getStorageSync(name);
    }
    return getCookie(name);
  }

  /**
   * 设置cookie
   *
   * @static
   * @param {string} name
   * @param {string} value
   * @param {number} [day=0]
   * @param {boolean} [isDomain=false]
   * @param {string} [path='/']
   * @memberof CookieUtil
   */
  static setCookie(
    name: string,
    value: string,
    day = 0,
    isDomain = false,
    path = '/',
  ): void {
    if (isUniMob()) {
      uni.setStorageSync(name, value);
    } else {
      setCookie(name, value, day, isDomain, path);
    }
  }
}
